﻿/*

========================================================================

	                        Light Prepass Renderer

		                        by Floris Groen

                         E-mail : fmfgroen@yahoo.com
                     Blog : www.florisgroen.blogspot.com
		      

========================================================================

*/

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace LightPrePassRenderer.Engine
{
    struct VertexFullScreenQuad
    {
        public Vector3 Position;
        public Vector3 TexCoordAndCornerIndex;

        public static VertexElement[] VertexElements =
        {
            new VertexElement
            (
                0,
                0,
                VertexElementFormat.Vector3,
                VertexElementMethod.Default,
                VertexElementUsage.Position,
                0
            ),
            new VertexElement
            (
                0,
                sizeof(float) * 3,
                VertexElementFormat.Vector3,
                VertexElementMethod.Default,
                VertexElementUsage.TextureCoordinate,
                0
            )
        };

        public const int SizeInBytes = sizeof(float) * 6;
    }

    public class FullScreenQuad
    {
        VertexDeclaration vertexDeclaration;
        VertexBuffer vertexBuffer;
        GraphicsDevice graphicsDevice;

        public FullScreenQuad(GraphicsDevice graphicsDevice)
        {
            this.graphicsDevice = graphicsDevice;

            vertexDeclaration = new VertexDeclaration(graphicsDevice, VertexFullScreenQuad.VertexElements);

            var vertices = new VertexFullScreenQuad[4];
            vertices[0].Position = new Vector3(1.0f, 1.0f, 0.0f);
            vertices[0].TexCoordAndCornerIndex = new Vector3(1, 0, 1);
            vertices[1].Position = new Vector3(1.0f, -1.0f, 0.0f);
            vertices[1].TexCoordAndCornerIndex = new Vector3(1, 1, 2);
            vertices[2].Position = new Vector3(-1.0f, 1.0f, 0.0f);
            vertices[2].TexCoordAndCornerIndex = new Vector3(0, 0, 0);
            vertices[3].Position = new Vector3(-1.0f, -1.0f, 0.0f);
            vertices[3].TexCoordAndCornerIndex = new Vector3(0, 1, 3);

            vertexBuffer = new VertexBuffer(graphicsDevice, 4 * VertexFullScreenQuad.SizeInBytes, BufferUsage.WriteOnly);
            vertexBuffer.SetData(vertices);
        }

        public void Draw()
        {
            graphicsDevice.VertexDeclaration = vertexDeclaration;
            graphicsDevice.Vertices[0].SetSource(vertexBuffer, 0, VertexFullScreenQuad.SizeInBytes);
            graphicsDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
        }
    }
}
